home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / newsgroups / misc.19981211-19990422 / 000383_news@watsun.cc.columbia.edu _Fri Mar 19 12:15:32 1999.msg < prev    next >
Internet Message Format  |  2020-01-01  |  4KB

  1. Return-Path: <news@watsun.cc.columbia.edu>
  2. Received: from newsmaster.cc.columbia.edu (newsmaster.cc.columbia.edu [128.59.59.30])
  3.     by watsun.cc.columbia.edu (8.8.5/8.8.5) with ESMTP id MAA22114
  4.     for <kermit.misc@watsun.cc.columbia.edu>; Fri, 19 Mar 1999 12:15:31 -0500 (EST)
  5. Received: (from news@localhost)
  6.     by newsmaster.cc.columbia.edu (8.8.5/8.8.5) id MAA21965
  7.     for kermit.misc@watsun.cc.columbia.edu; Fri, 19 Mar 1999 12:08:50 -0500 (EST)
  8. X-Authentication-Warning: newsmaster.cc.columbia.edu: news set sender to <news> using -f
  9. From: fdc@watsun.cc.columbia.edu (Frank da Cruz)
  10. Subject: Re: Associative Array in Kermit 95
  11. Date: 19 Mar 1999 17:08:49 GMT
  12. Organization: Columbia University
  13. Message-ID: <7cu0b1$lea$1@newsmaster.cc.columbia.edu>
  14. To: kermit.misc@watsun.cc.columbia.edu
  15.  
  16. In article <7cttsc$47f$1@nnrp1.dejanews.com>,  <dn5006@my-dejanews.com> wrote:
  17.  
  18. : Associative array is a very useful feature in scripting language, it enables
  19. : Perl, Tcl etc. the implementation of comlex data structures.  The following
  20. : script demonstrates that associative array can also be crafted in Kermit
  21. : 95. The script counts the unique words in a regular english, french, german,
  22. : etc. text file.
  23. : open read testfile.txt
  24. : if fail end 1 Can't open testfile.txt
  25. : assign \%n 0                                  ; init register
  26. : while true {
  27. :     read \%l                                  ; read each line
  28. :     if fail break                                ; until the end of file
  29. :     while > \flength(\%l) 0 {
  30. :         assign \%w \fbreak(\%l,{ })           ; split on space
  31. :         xif defined \m(\%w) {                 ; word already seen?
  32. :             _assign \%w \feval(\m(\%w) + 1)   ; incr count this word
  33. :         } else {
  34. :             _assign \%w 1                     ; init count this word
  35. :             increment \%n                     ; next register
  36. :             _assign \%n \%w                   ; register this word
  37. :         }
  38. :         assign \%l \fltrim(\fright(\%l,-
  39. :         \feval(\flength(\%l)-\flength(\%w)))) ; shift to next word
  40. :     }
  41. : }
  42. : for \%k 1 \%n 1 {
  43. :       assign \%w \m(\%k)                      ; get word from register
  44. :       echo <\m(\%w)> \%w                      ; display occurences
  45. : }
  46. : This approach avoids the use of array which has to be declared in advance.
  47. : The script does not take into account the non alphanumeric characters.
  48. : Dat Nguyen
  49. : Airline Telecommunications and Information Services
  50. : 770 Sherbrooke West
  51. : Montreal, Quebec
  52. : Canada H3A 1G1
  53. : Email dat.nguyen&sita.int
  54. Excellent!  I've had associative arrays on my list for quite a while, but
  55. the list so long and time so short.  I've reformatted your script to fit
  56. in 80 columns.
  57.  
  58. We plan to add a script library to the Kermit website -- this one will
  59. certainly go into it.  Other submissions are welcome too; send them in!
  60. (Be sure to document the Kermit program and version and other relevant
  61. info.)
  62.  
  63. A quick glance shows this script doesn't use any new (post-C-Kermit-6.0)
  64. features, some of which would make it simpler and faster, for example
  65. the new \fword() and \fsplit() functions for extracting words from strings,
  66. with specified break masks (e.g. to make sure punctuation does not count
  67. as part of word (e.g. "thing" and "thing.").  Also you can convert each
  68. word to lowercase with \flower() so "Thing", "thing", and "THING" count as
  69. the same word, etc.
  70.  
  71. Readers should take special note of the "_assign" verb, which is subtly
  72. different from "assign" (see p.457 of the manual).
  73.  
  74. - Frank